package ua.mykola.polishnotation;

import java.util.Deque;

import ua.mykola.calc.Calculator;

public class RPN2 implements RPN {
	private Calculator calc = new Calculator();

	public double eval(Deque<Object> input) {
		Object last = input.pollLast();
		if (isDouble(last)) {
			return Double.valueOf(last.toString());
		} else {
			Character c = (Character) last;
			double op2 = eval(input);
			double op1 = eval(input);
			return calc.calculate(c, op1, op2);
		}
	}

	private boolean isDouble(Object object) {
		try {
			Double.valueOf(object.toString());
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

}
